<template>
  <div>
    <el-dialog :visible.sync="dialogTableVisible" width="500px" append-to-body>
      <div slot="title">
        <span style="padding-right: 10px;">修改密码</span>
      </div>

      <el-form ref="form" :model="form" label-width="120px" :rules="rules">
        <el-form-item label="当前密码" prop="oldPass">
          <el-input
            type="password"
            style="width: 200px;"
            v-model="form.oldPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input
            type="password"
            style="width: 200px;"
            v-model="form.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="checkPass">
          <el-input
            type="password"
            style="width: 200px;"
            v-model="form.checkPass" autocomplete="off"></el-input>
        </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogTableVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  modifyPass
} from '@/api/user'

export default {
  name: 'ModifyPassword',
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.form.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };

    return {
      dialogTableVisible: false,

      form: {
        oldPass: '',
        pass: '',
        checkPass: ''
      },

      rules: {
        oldPass: [
          { required: true, message: '请输原来密码', trigger: 'blur' }
        ],

        pass: [
          { required: true, message: '请输新密码', trigger: 'blur' }
        ],
        checkPass: [
          { required: true, message: '请重复新密码', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ]
      }
    }
  },

  computed: {
  },

  methods: {
    showDialog() {
      this.dialogTableVisible = true
    },

    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (!valid) return false

        modifyPass(this.form).then(res => {
          this.$message({
            message: '修改成功！',
            type: 'success'
          })

          this.dialogTableVisible = false
        })
      })
    }
  }
}
</script>

<style scoped>
.message-title {
  font-size: 16px;
  color: #333;
  font-weight: bold;
  padding-right: 8px;
}
</style>
